<template>
    <h1>用户购买理财页面</h1>

    <table>
        <tr>
            <td>理财产品名称</td>
            <td>{{ Info.F_FName }}</td>
        </tr>

        <tr>
            <td>年收益</td>
            <td>{{ Info.F_YearMoney }}</td>
        </tr>

        <tr>
            <td>最低金额</td>
            <td>{{ Info.F_MinMoney }}</td>
        </tr>

        <tr>
            <td>风险等级</td>
            <td>{{ Number(Info.F_FinanLevel)==0 ?"低风险": Number(Info.F_FinanLevel)==1 ?"中风险" :"高风险"   }}</td>
        </tr>

        <tr>
            <td>购买金额</td>
            <td>
                <input type="text" v-model="UserMoney" @change="GetApyMoney" >
            </td>
        </tr>

        <tr>
            <td>每年预计收益</td>
            <td>
                {{ NMoney }}CNY
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="保存" @click="AddUserFinanData" >
            </td>
        </tr>

    </table>


</template>

<script setup lang="ts">
import { ref, onMounted} from 'vue';
import axios from 'axios';

import {useUserStore} from '../../store/index.ts';

import { useRouter,useRoute } from 'vue-router';

const route = useRoute();

const router = useRouter();

const userStore = useUserStore();

console.log(route.params.Id);//理财编号

console.log(userStore.UId);//用户编号

const Info = ref({
    F_FName:'',
    F_YearMoney:'',
    F_MinMoney:'',
    F_FinanLevel:'',
})

const UserMoney = ref('');
const NMoney = ref(0);

onMounted (()=>{
    SeraFinan()
})

const SeraFinan = ()=>{
    axios({
        method:'get',
        url:'http://localhost:63517/api/Finan/SeraFinan',
        params:{
            Id:route.params.Id
        }
    })
    .then(res=>{
        console.log(res.data);
        Info.value = res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}

const GetApyMoney = ()=>{
    axios({
        method:'get',
        url:'http://localhost:63517/api/Finan/GetApyMoney',
        params:{
            Money:UserMoney.value,
            FApy:Info.value.F_YearMoney
        }
    })
    .then(res=>{
        console.log(res.data);
        NMoney.value = res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}

const AddUserFinanData = ()=>{
    axios({
        method:'post',
        url:'http://localhost:63517/api/Finan/AddUserFinanData',
        data:{
            UF_Money:UserMoney.value,
            F_FId:route.params.Id,
            U_Id:userStore.UId

        }
    })
    .then(res=>{
        console.log(res.data);
        if(res.data>0){
            alert('用户理财成功');
            router.push('/UserFinanShow');
        }
        else{
            alert('用户理财失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}



</script>